package com.yellowpage.ca.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.map.LinkedMap;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.DefaultTransactionDefinition;

import com.yellowpage.white.common.Constants;
import com.yellowpage.white.common.PageRender;
import com.yellowpage.white.dao.QueryAssistantSQL;
import com.yellowpage.white.service.BaseServiceImpl;

import com.yellowpage.ca.data.UserFunction;
import com.yellowpage.ca.dao.UserFunctionDao;

public class UserFunctionServiceImpl extends BaseServiceImpl implements UserFunctionService {
	  private UserFunctionDao userFunctionDao;
	
	  public void setUserFunctionDao(UserFunctionDao userFunctionDao) {
		  this.userFunctionDao = userFunctionDao;
	  }
	
	
	  /* Smooth generate Method */

	public UserFunction getUserFunction(Long id){
	      return userFunctionDao.get(id);
	  }
	  
    public List<UserFunction> getAllUserFunction(){
        return userFunctionDao.getAll();
    }
    
    public List<Map> queryUserFunctionByPage(Map queryMap, PageRender pageRender) {		
        
		QueryAssistantSQL sqlAssistant = baseDao.newQueryAssistantSQL(

				"SELECT a.USER_FUNCTION_ID, "

				//给出用户名
				+ " (select user.display_name from CA_USER user where user.user_id = a.USER_ID) as USERNAME, "

			    //给出功能名
				+ " (select NAME from CA_FUNCTION where function_id = a.FUNCTION_ID) as FUNCTIONAME "

				+ "  FROM CA_USER_FUNCTION a  WHERE 1 = 1 ")

				//进行动态绑定 , CA_USER user , CA_FUNCTION f
				.addCondition(" and (select user.display_name from CA_USER user where user.user_id = a.USER_ID) like :userId  ")
				.addCondition(" and (select NAME from CA_FUNCTION where function_id = a.FUNCTION_ID) like :functionId ");
		    

		List<Map> resultList = sqlAssistant.queryByPage(
				baseDao.getConnection(), queryMap, pageRender);

		return resultList;
	}


	//给出功能的函数
	public Map getFunctionMap()

	{
		Map functionMap = new LinkedMap();

		QueryAssistantSQL sqlAssistant = baseDao
				.newQueryAssistantSQL("  select r.NAME, r.FUNCTION_ID as VALUE from CA_FUNCTION r ");

		List<Map> resultList = sqlAssistant.query(baseDao.getConnection(),
				new HashMap());

		for (int i = 0; i < resultList.size(); i++) {
			functionMap.put(resultList.get(i).get("VALUE"), resultList.get(i)
					.get("NAME"));
		}

		return functionMap;

	}

	//给出选人的函数
	public Map getUserMap() {
		Map userMap = new LinkedMap();

		QueryAssistantSQL sqlAssistant = baseDao
				.newQueryAssistantSQL("  select u.DISPLAY_NAME as NAME , u.USER_ID as VALUE " +
						"  from CA_USER u " +
						"  order by u.DISPLAY_NAME ");

		List<Map> resultList = sqlAssistant.query(baseDao.getConnection(),
				new HashMap());



		for (int i = 0; i < resultList.size(); i++) {
			userMap.put(resultList.get(i).get("VALUE"), resultList.get(i)
					.get("NAME"));
		}

		return userMap;
	}
    
    
      public void saveUserFunction(UserFunction userFunction){
        userFunctionDao.save(userFunction);
    }
    
	  public void updateUserFunction(UserFunction userFunction){
	      userFunctionDao.update(userFunction);
	  }
	  
	  public void delete(List deleteList){
	      userFunctionDao.remove(deleteList);
	  }
	  
	  
	 
	
}	